package com.alipay.freeblog.web.controller;

import com.alipay.freeblog.service.TemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * User: Ted
 * Date: 11-9-25
 * Time: 上午2:13
 */
@Controller
@RequestMapping("/template.htm")
public class TemplateManagenmentController {

	private TemplateService templateService;


	@RequestMapping(method = RequestMethod.GET, params = "do=list")
	public String list(String name, String templateName, ModelMap modelMap) throws Exception {
		if (name == null) {
			name = "";
		}
		modelMap.put("list", templateService.getTemplateList(name));
		if (templateName != null) {
			modelMap.put("template", templateService.getTemlate(templateName));
		}
		return "template/list";
	}

	@RequestMapping(method = RequestMethod.POST, params = "do=update")
	public String update(String templateName, String vm) throws Exception {
		if (templateName != null && !templateName.equals("")) {
			templateService.updateTemplate(templateName, vm);
			return "redirect:template.htm?do=list&templateName=" + templateName;
		} else {
			return "redirect:template.htm?do=list";
		}
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}
}
